package junior.排序;

public class 二分搜索技术 {
    public static void main(String[] args) {
        int[] sortArray = creatArray(100);
        boolean isExit = binarySearch(sortArray,49);
        System.out.println(isExit);

    }
    public static boolean binarySearch(int[] sortArray, int targetValue){
        int left = 0, right = sortArray.length-1, middle=0;
        while (left <= right){
            middle = (left + right) / 2;
            System.out.println(left +"\t"+ right+"\t"+ middle);
            if(sortArray[middle] > targetValue) {
                right = middle - 1;
            }else if(sortArray[middle] < targetValue){
                left = middle +1;
            }else {
                return true;
            }
        }
        return false;
    }
    public static int[] creatArray(int n){
        int[] sortArray = new int[n];
        for (int i = 0; i < n; i++) {
            sortArray[i] = i;
        }
        return sortArray;
    }
}
